
package solucionessgh.TipoHabTipoTarifa;

/**
 *
 * @author roderick
 */


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import solucionessgh.TipoHabitacion.TipoHabitacionModel;
import solucionessgh.TipoTarifa.TipoTarifaModel;
import solucionessgh.Util.ComboInfo;
import solucionessgh.Util.DataBase;


public class TipoHabTipoTarifaModel extends DataBase{
    private int id;
    private int thacod;
    private int ttacod;
    private double thtimp;
    //thacod,ttacod,thtimp
     public TipoHabTipoTarifaModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO tipohab_tipotarifa(thacod, ttacod, thtimp) VALUES (?, ?, ?);");
                stmt.setInt(1,this.thacod);
                stmt.setInt(2,this.ttacod);
                stmt.setDouble(3,this.thtimp);
                stmt.toString();
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE tipohab_tipotarifa SET thacod=?, ttacod=?, thtimp=? WHERE thtid = ? ;");
                stmt.setInt(1, this.thacod);
                stmt.setInt(2,this.ttacod);
                stmt.setDouble(3,this.thtimp);
                stmt.setInt(4, this.id);
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean delete(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from tipohab_tipotarifa where thtid=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT tp.tcbfec, tp.tcbcom, tp.tcbven,tp.id, m.mondesc as moncod FROM tipocambio tp INNER JOIN moneda m ON m.id=tp.moncod;");
    }

    public ResultSet finder(String var){
        return this.find("SELECT th.thadesc as thacod, tt.ttadesc as ttacod, thtt.thtimp, thtt.thtid   FROM tipohab_tipotarifa thtt INNER JOIN tipohabitacion th ON th.thacod=thtt.thacod INNER JOIN tipotarifas tt ON tt.ttacod=thtt.ttacod WHERE th.thadesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT thacod, ttacod, thtimp, thtid   FROM tipohab_tipotarifa WHERE thtid = "+var+";");
    }
    
    public void setComboTarifa(JComboBox combo){
        TipoTarifaModel model_tarifa = new TipoTarifaModel();
        ResultSet rs = model_tarifa.read();
        try {
            while ( rs.next()){
                combo.addItem(new ComboInfo(rs.getInt("ttacod"),rs.getString("ttadesc")));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        } 
    }
    
    public void setComboHabitacion(JComboBox combo){
        TipoHabitacionModel model_habitacion = new TipoHabitacionModel();
        ResultSet rs = model_habitacion.read();
        try {
            while ( rs.next()){
                combo.addItem(new ComboInfo(rs.getInt("thacod"),rs.getString("thadesc")));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        }
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getThacod() {
        return thacod;
    }

    public void setThacod(int thacod) {
        this.thacod = thacod;
    }

    public double getThtimp() {
        return thtimp;
    }

    public void setThtimp(double thtimp) {
        this.thtimp = thtimp;
    }

    public int getTtacod() {
        return ttacod;
    }

    public void setTtacod(int ttacod) {
        this.ttacod = ttacod;
    }

}
